#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 设置根节点
    ui->treeWidget->setHeaderLabel("动物");
    // 新增顶层节点
    QTreeWidgetItem* item1 = new QTreeWidgetItem();
    // 每一个节点都可以设置多个列，此处为了简单就只设置一列
    item1->setText(0, "猫");
    // 添加到顶层结点
    ui->treeWidget->addTopLevelItem(item1);


    QTreeWidgetItem* item2 = new QTreeWidgetItem();
    item2->setText(0, "狗");
    ui->treeWidget->addTopLevelItem(item2);

    QTreeWidgetItem* item3 = new QTreeWidgetItem();
    item3->setText(0, "鸟");
    ui->treeWidget->addTopLevelItem(item3);

    QTreeWidgetItem* item4 = new QTreeWidgetItem();
    item4->setText(0, "中华田园猫");
    item1->addChild(item4);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_top_clicked()
{
    const QString& text = ui->lineEdit->text();

    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);

    ui->treeWidget->addTopLevelItem(item);
}

void Widget::on_pushButton_inset_clicked()
{
    QTreeWidgetItem* currentItem = ui->treeWidget->currentItem();
    if(currentItem == nullptr)
    {
        return;
    }
    // 获取到输入框的内容
    const QString& text = ui->lineEdit->text();

    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);

    currentItem->addChild(item);

}

void Widget::on_pushButton_3_clicked()
{
    QTreeWidgetItem* currentItem = ui->treeWidget->currentItem();
    if(currentItem == nullptr)
    {
        return;
    }

    QTreeWidgetItem* parent = currentItem->parent();
    if(parent == nullptr)
    {
        int index = ui->treeWidget->indexOfTopLevelItem(currentItem);
    }
    else
    {
        parent->removeChild(currentItem);
    }


}
